package org.amphiprion.montrictracv4.web.util;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.preference.PreferenceManager;

public class PreferenceUtil {
	public static final String KEY_PREF_LANGUAGE = "pref_language";
	public static final String KEY_PREF_USER_GAME_DATA_1_LIST = "pref_user_game_data_1";
	public static final String KEY_PREF_USER_GAME_DATA_2_LIST = "pref_user_game_data_2";
	public static final String KEY_PREF_LANGUAGE_CHOICE = "pref_language_list";

	public static SharedPreferences pref;
	public static Context context;

	public static void init(Context context) {
		PreferenceUtil.context = context;
		PreferenceUtil.pref = PreferenceManager
				.getDefaultSharedPreferences(context);

		PreferenceUtil.setLanguage(PreferenceUtil.getLanguage());
	}

	public static String getLanguage() {
		return pref.getString(KEY_PREF_LANGUAGE, Locale.getDefault()
				.getLanguage());
	}

	public static void setLanguage(String lang) {
		Editor editor = pref.edit();
		editor.putString(KEY_PREF_LANGUAGE, lang);
		editor.commit();

		Locale locale2 = new Locale(lang);
		Locale.setDefault(locale2);
		Configuration config2 = new Configuration();
		config2.locale = locale2;
		context.getResources().updateConfiguration(config2,
				context.getResources().getDisplayMetrics());
	}
}